package com.eight.cloud.payment.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eight.cloud.core.global.exception.CustomException;
import com.eight.cloud.core.global.result.ResultCode;
import com.eight.cloud.mybatisplus.entity.BaseEntity;
import com.eight.cloud.mybatisplus.service.BaseService;
import com.eight.cloud.openapi.common.CacheKeyConstant;
import com.eight.cloud.payment.entity.MerchantAppEntity;
import com.eight.cloud.payment.mapper.MerchantAppMapper;
import com.eight.cloud.payment.model.vo.MerchantAppVo;
import com.eight.cloud.payment.service.IMerchantAppService;
import com.eight.cloud.redisson.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.eight.cloud.core.global.constant.NumConstant.ONE;
import static com.eight.cloud.core.global.constant.NumConstant.WEEK_SECONDS;
import static com.eight.cloud.payment.common.constant.CacheConstant.PAYMENT_MERCHANT_AUTH_APP_CACHE_KEY;

/**
*  服务实现类
*
* @ClassName: MerchantAppServiceImpl
* @Author TXC
* @Date 2025-01-07 17:22
*/
@Service
public class MerchantAppServiceImpl extends BaseService<MerchantAppMapper, MerchantAppEntity> implements IMerchantAppService {
    @Resource
    private RedisUtils redisUtils;

    @Override
    public boolean authorizeApp(String appid, String merchantId, String subAppid) {
        if (StrUtil.isBlank(appid) || StrUtil.isBlank(merchantId) || StrUtil.isBlank(subAppid)) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK);
        }
        MerchantAppEntity merchantAppEntity = baseMapper.selectOne(lambdaQueryWrapper().eq(MerchantAppEntity::getAppid, appid).eq(MerchantAppEntity::getMerchantId, merchantId));
        if (ObjectUtil.isNull(merchantAppEntity)) {
            return baseMapper.insert(new MerchantAppEntity().setAppid(appid).setMerchantId(merchantId).setSubAppid(subAppid)) > 0;
        }
        throw new CustomException(ResultCode.PARAM_TYPE_REPEAT);
    }

    @Override
    public boolean cancelAuthorizeApp(String appid, String merchantId) {
        if (StrUtil.isBlank(appid) || StrUtil.isBlank(merchantId)) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK);
        }
        baseMapper.delete(lambdaQueryWrapper().eq(MerchantAppEntity::getAppid, appid).eq(MerchantAppEntity::getMerchantId, merchantId));
        redisUtils.delete(CacheKeyConstant.generateKey(PAYMENT_MERCHANT_AUTH_APP_CACHE_KEY, merchantId, appid));
        return true;
    }

    @Override
    public List<MerchantAppEntity> getMerchantAppList(String merchantId) {
        if (StrUtil.isBlank(merchantId)) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK);
        }
        return baseMapper.selectList(lambdaQueryWrapper().eq(MerchantAppEntity::getMerchantId, merchantId));
    }

    @Override
    public boolean isAuthorizeApp(String appid, String merchantId) {
        MerchantAppVo merchantApp = getAuthorizeApp(appid, merchantId);
        return ObjectUtil.isNotNull(merchantApp);
    }

    @Override
    public MerchantAppVo getAuthorizeApp(String appid, String merchantId) {
        if (StrUtil.isBlank(appid) || StrUtil.isBlank(merchantId)) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK);
        }
        MerchantAppVo authorizeApp = (MerchantAppVo)redisUtils.get(CacheKeyConstant.generateKey(PAYMENT_MERCHANT_AUTH_APP_CACHE_KEY, merchantId, appid));
        if (ObjectUtil.isNotNull(authorizeApp)) {
            return authorizeApp;
        }
        authorizeApp = convertToObject(baseMapper.selectOne(lambdaQueryWrapper().eq(MerchantAppEntity::getAppid, appid)
                .eq(MerchantAppEntity::getMerchantId, merchantId).eq(BaseEntity::getStatus, ONE)), MerchantAppVo.class);
        if (ObjectUtil.isNotNull(authorizeApp)) {
            redisUtils.set(CacheKeyConstant.generateKey(PAYMENT_MERCHANT_AUTH_APP_CACHE_KEY, merchantId, appid), authorizeApp, WEEK_SECONDS);
            return authorizeApp;
        }
        return null;
    }
}
